package com.open.capacity.reimburse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.reimburse.dao.BillBusinesstrip2Dao;
import com.open.capacity.reimburse.dao.BillTraining2Dao;
import com.open.capacity.reimburse.dto.report.BusinesstripBcDto;
import com.open.capacity.reimburse.dto.report.BusinesstripUsageDto;
import com.open.capacity.reimburse.dto.report.TrainingUsageDto;
import com.open.capacity.reimburse.entity.BillBusinesstrip2;
import com.open.capacity.reimburse.entity.BillBusinesstrip2Cost;
import com.open.capacity.reimburse.entity.BillTraining2;
import com.open.capacity.reimburse.entity.BillTraining2Cost;
import com.open.capacity.reimburse.service.*;
import com.open.capacity.user.service.SysUserService;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.StringUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Service
public class BillReportServiceImpl implements BillReportService {

    @Autowired
    private BillBusinesstrip2Dao businesstrip2Dao;

    @Autowired
    private BillTraining2Dao training2Dao;

    @Autowired
    private SysUserService userService;


    @Override
    public PageResult<BusinesstripBcDto> pageBusinesstripBcb(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillBusinesstrip2> list = businesstrip2Dao.findReport(params);
        List<BusinesstripBcDto> dtoList = new ArrayList<>();
        list.forEach(item -> {
            BusinesstripBcDto dto = new BusinesstripBcDto();
            dto.setDescription(item.getDescription());
            dto.setDestinationStr(item.getDestinationStr());
            int days = DateUtil.getBetweenDay(item.getStartTime(), item.getEndTime());
            int personCounts = (StringUtil.isEmpty(item.getFollowUserids()) ? 0 : item.getFollowUserids().split(",").length) + 1;
            dto.setDays(days);
            dto.setPersons(personCounts);
            dto.setTotalFee(item.getCost());
            BigDecimal holtelFee = BigDecimal.ZERO, trafficFee1 = BigDecimal.ZERO, trafficFee2 = BigDecimal.ZERO, mealFee = BigDecimal.ZERO, otherFee = BigDecimal.ZERO;
            for (BillBusinesstrip2Cost detail : item.getCostList()) {
                switch (detail.getItemCode()) {
                    case "1": //住宿费
                        holtelFee = holtelFee.add(detail.getMoney());
                        break;
                    case "2": //'机票费'
                    case "3": //'火车票'
                        trafficFee1 = trafficFee1.add(detail.getMoney());
                        break;
                    case "4": //'伙食补助'
                        mealFee = mealFee.add(detail.getMoney());
                        break;
                    case "5": //'市内交通补助'
                        trafficFee2 = trafficFee2.add(detail.getMoney());
                        break;
                    case "99": //'其他费'
                        otherFee = otherFee.add(detail.getMoney());
                        break;
                }
            }
            dto.setHotelFee(holtelFee);
            dto.setTrafficFee1(trafficFee1);
            dto.setTrafficFee2(trafficFee2);
            dto.setMealFee(mealFee);
            dto.setOtherFee(otherFee);
            dto.setPerFee(item.getCost().divide(BigDecimal.valueOf(personCounts), 2));
            dto.setFollowUsernames(item.getUsername() + (StringUtil.isEmpty(item.getFollowUsernames()) ? "" : ("," + item.getFollowUsernames())));
            dto.setDepartmentName(userService.findById(item.getUserid()).getDepartmentName());
            dtoList.add(dto);
        });
        PageInfo<BusinesstripBcDto> pageInfo = new PageInfo(dtoList);

        return PageResult.<BusinesstripBcDto>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    @Override
    public List<BusinesstripBcDto> findBusinesstripBcb(Map<String, Object> params) {
        List<BusinesstripBcDto> dtoList = new ArrayList<>();
        List<BillBusinesstrip2> list = businesstrip2Dao.findReport(params);
        list.forEach(item -> {
            BusinesstripBcDto dto = new BusinesstripBcDto();
            dto.setDescription(item.getDescription());
            dto.setDestinationStr(item.getDestinationStr());
            int days = DateUtil.getBetweenDay(item.getStartTime(), item.getEndTime());
            int personCounts = (StringUtil.isEmpty(item.getFollowUserids()) ? 0 : item.getFollowUserids().split(",").length) + 1;
            dto.setDays(days);
            dto.setPersons(personCounts);
            dto.setTotalFee(item.getCost());
            BigDecimal holtelFee = BigDecimal.ZERO, trafficFee1 = BigDecimal.ZERO, trafficFee2 = BigDecimal.ZERO, mealFee = BigDecimal.ZERO, otherFee = BigDecimal.ZERO;
            for (BillBusinesstrip2Cost detail : item.getCostList()) {
                switch (detail.getItemCode()) {
                    case "1": //住宿费
                        holtelFee = holtelFee.add(detail.getMoney());
                        break;
                    case "2": //'机票费'
                    case "3": //'火车票'
                        trafficFee1 = trafficFee1.add(detail.getMoney());
                        break;
                    case "4": //'伙食补助'
                        mealFee = mealFee.add(detail.getMoney());
                        break;
                    case "5": //'市内交通补助'
                        trafficFee2 = trafficFee2.add(detail.getMoney());
                        break;
                    case "99": //'其他费'
                        otherFee = otherFee.add(detail.getMoney());
                        break;
                }
            }
            dto.setHotelFee(holtelFee);
            dto.setTrafficFee1(trafficFee1);
            dto.setTrafficFee2(trafficFee2);
            dto.setMealFee(mealFee);
            dto.setOtherFee(otherFee);
            dto.setPerFee(item.getCost().divide(BigDecimal.valueOf(personCounts)));
            dto.setFollowUsernames(item.getUsername() + (StringUtil.isEmpty(item.getFollowUsernames()) ? "" : ("," + item.getFollowUsernames())));
            dto.setDepartmentName(userService.findById(item.getUserid()).getDepartmentName());
            dtoList.add(dto);
        });
        return dtoList;
    }

    @Override
    public PageResult<BusinesstripUsageDto> pageBusinesstripUsage(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillBusinesstrip2> list = businesstrip2Dao.findReport(params);
        List<BusinesstripUsageDto> dtoList = new ArrayList<>();
        list.forEach(item -> {
            BusinesstripUsageDto dto = new BusinesstripUsageDto();
            dto.setReimburseMoney(item.getCost());
            dto.setDescription(item.getDescription());
            dto.setDestinationStr(item.getDestinationStr());
            int days = DateUtil.getBetweenDay(item.getStartTime(), item.getEndTime());
            int personCounts = (StringUtil.isEmpty(item.getFollowUserids()) ? 0 : item.getFollowUserids().split(",").length) + 1;
            dto.setDays(days);
            dto.setPersons(personCounts);
            dto.setTotalFee(item.getCost());
            BigDecimal holtelFee = BigDecimal.ZERO, trafficFee1 = BigDecimal.ZERO, trafficFee2 = BigDecimal.ZERO, mealFee = BigDecimal.ZERO, otherFee = BigDecimal.ZERO;
            for (BillBusinesstrip2Cost detail : item.getCostList()) {
                switch (detail.getItemCode()) {
                    case "1": //住宿费
                        holtelFee = holtelFee.add(detail.getMoney());
                        break;
                    case "2": //'机票费'
                    case "3": //'火车票'
                        trafficFee1 = trafficFee1.add(detail.getMoney());
                        break;
                    case "4": //'伙食补助'
                        mealFee = mealFee.add(detail.getMoney());
                        break;
                    case "5": //'市内交通补助'
                        trafficFee2 = trafficFee2.add(detail.getMoney());
                        break;
                    case "99": //'其他费'
                        otherFee = otherFee.add(detail.getMoney());
                        break;
                }
            }
            dto.setHotelFee(holtelFee);
            dto.setTrafficFee1(trafficFee1);
            dto.setTrafficFee2(trafficFee2);
            dto.setMealFee(mealFee);
            dto.setOtherFee(otherFee);

            dtoList.add(dto);
        });
        PageInfo<BusinesstripUsageDto> pageInfo = new PageInfo(dtoList);

        return PageResult.<BusinesstripUsageDto>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();

    }

    @Override
    public List<BusinesstripUsageDto> findBusinesstripUsage(Map<String, Object> params) {
        List<BusinesstripUsageDto> dtoList = new ArrayList<>();
        List<BillBusinesstrip2> list = businesstrip2Dao.findReport(params);
        list.forEach(item -> {
            BusinesstripUsageDto dto = new BusinesstripUsageDto();
            dto.setReimburseMoney(item.getCost());
            dto.setDescription(item.getDescription());
            dto.setDestinationStr(item.getDestinationStr());
            int days = DateUtil.getBetweenDay(item.getStartTime(), item.getEndTime());
            int personCounts = (StringUtil.isEmpty(item.getFollowUserids()) ? 0 : item.getFollowUserids().split(",").length) + 1;
            dto.setDays(days);
            dto.setPersons(personCounts);
            dto.setTotalFee(item.getCost());
            BigDecimal holtelFee = BigDecimal.ZERO, trafficFee1 = BigDecimal.ZERO, trafficFee2 = BigDecimal.ZERO, mealFee = BigDecimal.ZERO, otherFee = BigDecimal.ZERO;
            for (BillBusinesstrip2Cost detail : item.getCostList()) {
                switch (detail.getItemCode()) {
                    case "1": //住宿费
                        holtelFee = holtelFee.add(detail.getMoney());
                        break;
                    case "2": //'机票费'
                    case "3": //'火车票'
                        trafficFee1 = trafficFee1.add(detail.getMoney());
                        break;
                    case "4": //'伙食补助'
                        mealFee = mealFee.add(detail.getMoney());
                        break;
                    case "5": //'市内交通补助'
                        trafficFee2 = trafficFee2.add(detail.getMoney());
                        break;
                    case "99": //'其他费'
                        otherFee = otherFee.add(detail.getMoney());
                        break;
                }
            }
            dto.setHotelFee(holtelFee);
            dto.setTrafficFee1(trafficFee1);
            dto.setTrafficFee2(trafficFee2);
            dto.setMealFee(mealFee);
            dto.setOtherFee(otherFee);

            dtoList.add(dto);
        });
        return dtoList;
    }

    @Override
    public PageResult<TrainingUsageDto> pageTrainingUsage(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillTraining2> list = training2Dao.findReport(params);
        List<TrainingUsageDto> dtoList = new ArrayList<>();
        list.forEach(item -> {
            TrainingUsageDto dto = new TrainingUsageDto();
            dto.setReimburseMoney(item.getCost());
            int days = DateUtil.getBetweenDay(item.getStartTime(), item.getEndTime());
            dto.setDays(days);
            dto.setAttendCount(item.getAttendCount());
            dto.setTotalFee(item.getCost());
            BigDecimal holtelFee = BigDecimal.ZERO, trafficFee = BigDecimal.ZERO, teacherFee = BigDecimal.ZERO, mealFee = BigDecimal.ZERO, siteFee = BigDecimal.ZERO, otherFee = BigDecimal.ZERO;
            for (BillTraining2Cost detail : item.getCostList()) {
                switch (detail.getItemCode()) {
                    case "1": //住宿费
                        holtelFee = holtelFee.add(detail.getMoney());
                        break;
                    case "2": //'伙食费'
                        mealFee = mealFee.add(detail.getMoney());
                        break;
                    case "6": //'场地费'
                        siteFee = siteFee.add(detail.getMoney());
                        break;
                    case "7": //'师资费(正高)'
                    case "8": //'师资费(副高)'
                    case "9": //'师资费(其他)'
                        teacherFee = teacherFee.add(detail.getMoney());
                        break;
                    case "99": //'其他费'
                        otherFee = otherFee.add(detail.getMoney());
                        break;
                }
            }
            dto.setHotelFee(holtelFee);
            dto.setTrafficFee(trafficFee);
            dto.setTeacherFee(teacherFee);
            dto.setJoinFee(BigDecimal.ZERO);
            dto.setMealFee(mealFee);
            dto.setSiteFee(siteFee);
            dto.setOtherFee(otherFee);

            dtoList.add(dto);
        });
        PageInfo<TrainingUsageDto> pageInfo = new PageInfo(dtoList);

        return PageResult.<TrainingUsageDto>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    @Override
    public List<TrainingUsageDto> findTrainingUsage(Map<String, Object> params) {
        List<TrainingUsageDto> dtoList = new ArrayList<>();
        List<BillTraining2> list = training2Dao.findReport(params);
        list.forEach(item -> {
            TrainingUsageDto dto = new TrainingUsageDto();
            dto.setReimburseMoney(item.getCost());
            int days = DateUtil.getBetweenDay(item.getStartTime(), item.getEndTime());
            dto.setDays(days);
            dto.setAttendCount(item.getAttendCount());
            dto.setTotalFee(item.getCost());
            BigDecimal holtelFee = BigDecimal.ZERO, trafficFee = BigDecimal.ZERO, teacherFee = BigDecimal.ZERO, mealFee = BigDecimal.ZERO, siteFee = BigDecimal.ZERO, otherFee = BigDecimal.ZERO;
            for (BillTraining2Cost detail : item.getCostList()) {
                switch (detail.getItemCode()) {
                    case "1": //住宿费
                        holtelFee = holtelFee.add(detail.getMoney());
                        break;
                    case "2": //'伙食费'
                        mealFee = mealFee.add(detail.getMoney());
                        break;
                    case "6": //'场地费'
                        siteFee = siteFee.add(detail.getMoney());
                        break;
                    case "7": //'师资费(正高)'
                    case "8": //'师资费(副高)'
                    case "9": //'师资费(其他)'
                        teacherFee = teacherFee.add(detail.getMoney());
                        break;
                    case "99": //'其他费'
                        otherFee = otherFee.add(detail.getMoney());
                        break;
                }
            }
            dto.setHotelFee(holtelFee);
            dto.setTrafficFee(trafficFee);
            dto.setTeacherFee(teacherFee);
            dto.setJoinFee(BigDecimal.ZERO);
            dto.setMealFee(mealFee);
            dto.setSiteFee(siteFee);
            dto.setOtherFee(otherFee);

            dtoList.add(dto);
        });
        return dtoList;
    }
}
